import numpy as np


def loadDataset(fileName):
    firstlineArray = open(fileName).readline().split("\t")
    indexCounty = firstlineArray.index('dwv_order_make_haikou_6.county')
    indexDate = firstlineArray.index('dwv_order_make_haikou_6.departure_time')
    # 定义循环计数
    loopIndex = 0
    # 最终输出的结果
    dataMat = []
    fo = open(fileName)
    for oneline in fo.readlines():
        extractedOneLineArray = []
        # 跳过首行
        if loopIndex != 0:
            onelineArray = oneline.split("\t")
            extractedOneLineArray.append(onelineArray[indexCounty])
            extractedOneLineArray.append(onelineArray[indexDate])
            dataMat.append(extractedOneLineArray)
        loopIndex = loopIndex + 1
    print(len(dataMat))
    return dataMat


def processDataToRegions(dataMat):
    region1=[]
    region2=[]
    region3=[]
    region4=[]
    for i in range(len(dataMat)):
        if dataMat[i][0]=='460105':
            region1.append(dataMat[i])
        elif dataMat[i][0]=='460106':
            region2.append(dataMat[i])
        elif dataMat[i][0] == '460107':
            region3.append(dataMat[i])
        elif dataMat[i][0] == '460108':
            region4.append(dataMat[i])
    return region1,region2,region3,region4


def processDatabyDate(regionData):
    for i in range(len(regionData)):
        # 仅保留月份日期
        newDateMonth=regionData[i][1].split(" ")[0].split("-")[1]  # '06'
        newDateDay=regionData[i][1].split(" ")[0].split("-")[2]  # '17'
        if newDateMonth[0] == '0':
            newDateMonth = newDateMonth[1:]
        if newDateDay[0]=='0':
            newDateDay=newDateDay[1:]
        # 日期格式：月*31+日
        newDate=str(int(newDateMonth)*31+int(newDateDay))
        regionData[i][1]=newDate
    for regionDatum in regionData:
        print(regionDatum)
    regionMat=np.mat(regionData).astype(np.int16)
    # 获取最小日期
    minDate=min(regionMat[:,1])[0,0]+1
    # 日期索引
    index=minDate
    # 将日期标准化到从第一天开始
    normalIndex=1
    # 结果格式(第几天,车流量)
    finalDateInfo=[]
    # 仅读取前15天流量
    while index<minDate+19:
        count=0
        for i in range(regionMat.shape[0]):
            if regionMat[i,1]==index:
                count=count+1
        print("Date "+str(index)+": "+str(count))
        oneLineArray= [normalIndex, count]
        finalDateInfo.append(oneLineArray)
        index=index+1
        normalIndex=normalIndex+1
    print(finalDateInfo)
    return finalDateInfo






dataMat = loadDataset('./original_datasets/dwv_order_make_haikou_6.txt')
region1,region2,region3,region4=processDataToRegions(dataMat)
# region1DateInfo=processDatabyDate(region1)
region2DateInfo=processDatabyDate(region2)
# region3DateInfo=processDatabyDate(region3)
# region4DateInfo=processDatabyDate(region4)
# [日期 流量]
# np.savetxt('./processed_datasets/region13.txt',region1DateInfo,fmt='%d',delimiter=' ',newline='\n')
np.savetxt('./processed_datasets/region14.txt',region2DateInfo,fmt='%d',delimiter=' ',newline='\n')
# np.savetxt('./processed_datasets/region15.txt',region3DateInfo,fmt='%d',delimiter=' ',newline='\n')
# np.savetxt('./processed_datasets/region16.txt',region4DateInfo,fmt='%d',delimiter=' ',newline='\n')


